package com.exampleyyy.demoyyy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.exampleyyy.demoyyy.JieKouService.AddressBookServiceJieKou;
import com.exampleyyy.demoyyy.SHITIYYY.AddressBook;
import com.exampleyyy.demoyyy.common.BaseContext;
import com.exampleyyy.demoyyy.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 地址簿管理
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired  // 装配地址服务层接口
    private AddressBookServiceJieKou addressbookservicejiekou;

    /**
     * 新增
     */
    @PostMapping("")
    public R<Object> save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}", addressBook);
        addressbookservicejiekou.save(addressBook);
        return R.success(1,"地址保存成功",addressBook);
    }
    @GetMapping("/list")
    public R<Object> list(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}", addressBook);

        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        //SQL:select * from address_book where user_id = ? order by update_time desc
        return R.success(1,"查询全部地址",addressbookservicejiekou.list(queryWrapper));
    }
    /**
     * 修改默认地址
     */
    @PutMapping("/default")
    public R<Object> setDefault(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        wrapper.set(AddressBook::getIsDefault, 0);
        //SQL:update address_book set is_default = 0 where user_id = ?
        addressbookservicejiekou.update(wrapper);
        addressBook.setIsDefault(1);
        //SQL:update address_book set is_default = 1 where id = ?
        addressbookservicejiekou.updateById(addressBook);
        return R.success(1,"默认地址设置成功",addressBook);
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public R<Object> get(@PathVariable Long id) {
        AddressBook addressBook = addressbookservicejiekou.getById(id);
        if (addressBook != null) {
            return R.success(1,"根据id查询成功",addressBook);
        } else {
            return R.error(0,"没有找到该对象",null);
        }
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public R<Object> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);
        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressbookservicejiekou.getOne(queryWrapper);

        if (null == addressBook) {
            return R.error(0,"没有找到该对象",null);
        } else {
            return R.success(1,"查询默认地址成功",addressBook);
        }
    }


    @PutMapping("")// 修改地址接口
    public R<Object> update(@RequestBody AddressBook addressBook){
        try{
            addressbookservicejiekou.updateById(addressBook);
            return R.success(1,"修改地址信息成功",null);
        } catch (Exception eeee){
            System.out.println("修改地址的错误是"+eeee);
            return R.error(0,"修改地址失败",null);
        }
    }
}